package com.mediacloud.app.newsmodule.view

import android.annotation.SuppressLint
import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import com.baidu.location.BDLocation
import com.eventTrack.TXReportManager
import com.mediacloud.app.assembly.common.BaseIJsonParseable
import com.mediacloud.app.assembly.interfaces.LoadNetworkBack
import com.mediacloud.app.assembly.util.load
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.model.interfaces.ILocation
import com.mediacloud.app.model.interfaces.IPermissionsAgree
import com.mediacloud.app.model.interfaces.PermissionUtil
import com.mediacloud.app.model.interfaces.PermissionUtil.getLocation
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.activity.WeatherDetailH5Activity
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig
import com.mediacloud.app.reslib.util.DataInvokeUtil
import org.json.JSONObject

class WeatherSetter(val view: View) {
    var city: TextView = view.findViewById(R.id.city)
    var weatherTx: TextView = view.findViewById(R.id.weather)
    var temperature: TextView = view.findViewById(R.id.temperature)
    var weatherIcon: ImageView = view.findViewById(R.id.weather_icon)
    var noData: View = view.findViewById(R.id.noData)

    init {
        initView()
    }

    private fun initView() {
        city.visibility = View.INVISIBLE
        if (TextUtils.isEmpty(AppFactoryGlobalConfig.getLatitude())) {
            setNoData(true)
            view.setOnClickListener {

                PermissionUtil.invokeLocationPermission(
                    view.context as FragmentActivity,
                    object : IPermissionsAgree {
                        override fun onAgree() {
                            getLocation(view.context, object : ILocation {
                                override fun onLocation(location: BDLocation) {
                                    getWeather()
                                }

                                override fun onFail() {
                                }
                            })
                        }

                        override fun onRefuse() {

                        }

                        override fun hasPermissions() {
                            getLocation(view.context, object : ILocation {
                                override fun onLocation(location: BDLocation) {
                                    getWeather()
                                }

                                override fun onFail() {
                                    if (!PermissionUtil.isGpsEnabled(view.context)) {
                                        PermissionUtil.showGPSPop(
                                            view.context,
                                            "定位服务未开启",
                                            "请打开GPS定位服务"
                                        )
                                    }
                                }
                            })
                        }
                    })
            }
        } else {
            getWeather()
        }
    }

    fun getWeather() {
        DataInvokeUtil.getWeather(
            AppFactoryGlobalConfig.getLatitude(),
            AppFactoryGlobalConfig.getLongitude(),
            "0",
            "xinzhi",
            object : LoadNetworkBack<BaseIJsonParseable> {
                override fun Success(result: BaseIJsonParseable) {
                    if (result.state) {
                        optData(result.orginData.optJSONObject("data"))
                    } else {
                        setNoData(false)
                    }
                }

                override fun Failure(obj: Any) {
                    setNoData(false)
                }

                override fun otherData(result: String) {}
            },
            BaseIJsonParseable()
        )
    }

    @SuppressLint("SetTextI18n")
    private fun optData(jsonObject: JSONObject?) {
        noData.visibility = View.GONE
        city.visibility = View.VISIBLE
        weatherTx.visibility = View.VISIBLE
        val detail = jsonObject?.optString("xz_weather", "")
        val todayObject: JSONObject? = jsonObject?.optJSONObject("today")
        city.text = todayObject?.optString("path")
        AppFactoryGlobalConfig.getAddress()?.let {
            if (!TextUtils.isEmpty(it.city)) {
                city.text = "${it.city} ${it.district}"
            }
        }
        city.visibility = View.VISIBLE
        weatherTx.text = todayObject?.optString("weather")
        temperature.text = "${todayObject?.optString("temperature")}°"
        val icon = todayObject?.optString("weather_icon")
        weatherIcon.loadX(icon, 1, 0, false, null)
        //  GlideUtils.loadUrl(icon, weatherIcon, null, AppFactoryGlobalConfig.getDefaultImageLoadDrawable())
        view.setOnClickListener {
            val intent = Intent(view.context, WeatherDetailH5Activity::class.java)
            intent.putExtra("WEATHER_URL", detail)
            view.context.startActivity(intent)
        }
        try {
            TXReportManager.getInstance().view_weather(city.text.toString())
        } catch (e: Exception) {

        }

    }

    private fun setNoData(noLocation: Boolean) {
        if (noLocation) {
            city.text = "未获取位置"
        } else {
            AppFactoryGlobalConfig.getAddress()?.let {
                city.visibility = View.VISIBLE
                city.text = "${it.city} ${it.district}"
            }
            weatherTx.visibility = View.GONE
            noData.visibility = View.VISIBLE
        }

        weatherIcon.setImageResource(R.drawable.weather_no_data)
        city.visibility = View.VISIBLE
    }
}